package com.gm.commonlib.base.baseui.baseListviewUI;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gm.commonlib.R;
import com.gm.commonlib.views.toast.ToastUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


public abstract class BaseListFragment<
        DataType,
        NetworkResponse> extends BasePullToRefeshFragment<
                DataType,
                NetworkResponse> {

    public ListView pullToRefreshListView;
    public TextView tv_no_content;
    public RefreshLayout mRefreshLayout;

    private FrameLayout ll_latest_cargos;


    protected void notifyListDataSetChanged() {
        if (mBaseAdapter != null) {
            mBaseAdapter.notifyDataSetChanged();
        }
        if (mDatas.isEmpty()) {
            tv_no_content.setVisibility(View.VISIBLE);
        } else {
            tv_no_content.setVisibility(View.GONE);
        }
    }
    public void setBodybg(int color){
        ll_latest_cargos.setBackgroundColor(color);
    }
    public void setBackgroundResource(int id){
        ll_latest_cargos.setBackgroundResource(id);
    }

    public void initPullToRefreshListView() {
        mBaseAdapter = createTempBaseAdapter();
        pullToRefreshListView.setAdapter(mBaseAdapter);
        setPullToRefreshListViewListener();
    }

    public void setPullToRefreshListViewListener() {

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                onRefreshView();
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                loadMore();
            }
        });
    }


    public void loadMore() {
        if (mPageInfo != null) {
            if (mCurrentPage >= mPageInfo.getTotalPages()) {
                ToastUtil.showToast(getResources().getString(R.string.already_last_page));
                getDataOnCompleted();
            } else {
                requestPageData(mCurrentPage + 1, false);
            }
        } else {
            requestPageData(1, false);
        }
    }
    @Override
    public void getDataOnCompleted() {
        super.getDataOnCompleted();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.base_list_pulltorefresh, parent);
        pullToRefreshListView = view.findViewById(R.id.listView);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        tv_no_content = view.findViewById(R.id.tv_no_content);
        ll_latest_cargos = view.findViewById(R.id.ll_latest_cargos);
    }

    protected void setRefreshAndLoadEnable(boolean isPull, boolean isLoadMore) {
        if (mPageInfo == null) {
            setRefreshView(true, false);
            return;
        }
        if (isLastPagerData(mPageInfo) || mDatas.isEmpty()) {
            setRefreshView(true, false);
        } else {
            setRefreshView(true, true);
        }
    }

    protected void setRefreshView(boolean isPull, boolean isLoadMore) {
        mRefreshLayout.setEnableLoadMore(isLoadMore);
        mRefreshLayout.setEnableRefresh(isPull);
        mRefreshLayout.setEnableAutoLoadMore(isLoadMore);

    }


    public void setNoContentText(String text) {
        tv_no_content.setText(text);
    }


}
